<template>
  <div class="login-wrapper">
    <div class="modal">
      <el-form ref="userForm" :model="user" :rules="rules" status-icon>
        <div class="title">lazy</div>
        <el-form-item prop="userName">
          <el-input
            type="text"
            v-model="user.userName"
            prefix-icon="el-icon-user"
            placeholder="请输入账号"
          />
        </el-form-item>
        <el-form-item prop="userPassword">
          <el-input
            type="password"
            v-model="user.userPassword"
            prefix-icon="el-icon-view"
            placeholder="请输入密码"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="btn-login" @click="login"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: "login",

  data() {
    return {
      user: {
        userName: "",
        userPassword: "",
      },

      // 校验信息
      rules: {
        userName: [
          {
            required: true,
            message: "账号不能为空",
            trigger: "blur",
          },
        ],
        userPassword: [
          {
            required: true,
            message: "密码不能为空",
            trigger: "blur",
          },
        ],
      },
    };
  },

  created() {
    // console.log(this.$store.state.userInfo);
  },

  methods: {
    // 登录
    login() {
      // 调用校验函数
      this.$refs.userForm.validate(async (valid) => {
        if (!valid) {
          return false;
        }
        const res = await this.$api.login();
        this.$store.commit("saveUserInfo", res);
        this.$router.push("/");
      });
    },
  },
};
</script>

<style lang="scss">
.login-wrapper {
  width: 100vw;
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(to top, #e0eafc, #cfdef3);
  .modal {
    padding: 20px 40px 0;
    width: 400px;
    height: 280px;
    border-radius: 6px;
    background-color: #fff;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
    .title {
      margin-bottom: 20px;
      font-size: 24px;
      text-align: center;
    }
    .btn-login {
      width: 100%;
      font-size: 16px;
    }
  }
}
</style>
